#include "widget_student_list_view_item.h"
#include "ui_widget_student_list_view_item.h"
#include <QDebug>
#include <QTextFormat>
#include <QPalette>
#include <QGradient>
#include <QMouseEvent>
#include <QStyle>

widget_student_list_view_item::widget_student_list_view_item(
            QWidget *parent,QString studentName,
            QString testStartTime, QString testName,
            int completionPercent, int confidencePercent) :
    QWidget(parent),
    ui(new Ui::widget_student_list_view_item)
{

    ui->setupUi(this);
    if (!studentName.isNull()){
        ui->StudentName->setText(studentName);
    }
    if (!testStartTime.isNull()){
        ui->TestStartTime->setText(testStartTime);
    }
    if (!testName.isNull()){
        ui->TestName->setText(testName);
    }
    if (completionPercent!=-1){

        ui->CompletionPercent->setText(QString::number(completionPercent) + "%");
    }
    if (confidencePercent!=-1){
        if (confidencePercent < 50){
            ui->ConfidencePercent->setText(
                        "<font color=#FF0000>"+
                        QString::number(confidencePercent)+
                        "%"+
                        "</font>");
        } else{
            ui->ConfidencePercent->setText(QString::number(confidencePercent) + "%");
        }
    }

    QPalette qp = ui->frame->palette();
    QSize qsize = ui->frame->size();
    QLinearGradient qg(qsize.width()/2,0,qsize.width()/2,qsize.height());
    qg.setColorAt(0, QColor(220,220,220));
    qg.setColorAt(1.0, QColor(150,150,150));
    QBrush brush(qg);
    qp.setBrush(QPalette::Window,  brush);


    ui->frame->setPalette(qp);
    ui->frame->update();


}

void widget_student_list_view_item::mousePressEvent(QMouseEvent *){
    qDebug("MOUSEPRESSED");
    this->mousedown = true;
}

void widget_student_list_view_item::mouseMoveEvent(QMouseEvent * e){
    qDebug() << "MOUSEMOVE x: " << e->x() <<" y: " << e->y();
    QSize s = this->size();
    if (e->y() < 0 || e->y() > s.height() || e->x() < 0 || e->x() > s.width()){
        mousedown = false;
    }
}

void widget_student_list_view_item::mouseReleaseEvent(QMouseEvent * e){
    if (mousedown == true){
//        emit(clicked(e));
    }
    mousedown = false;

}

widget_student_list_view_item::~widget_student_list_view_item()
{
    delete ui;
}


void widget_student_list_view_item::on_viewDetails_clicked()
{
    emit(clicked(0));
}

void widget_student_list_view_item::hide_chat_icon()
{
    ui->chatlabel->hide();
}
